<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>验收单据查询</title>
    <link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.3.3/themes/default/easyui.css"></link>
    <link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.3.3/themes/icon.css"></link>
    <script type="text/javascript" src="/static/jquery-easyui-1.3.3/jquery.min.js"></script>
    <script type="text/javascript" src="/static/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>

    <script type="text/javascript" src="/static/js/date.js"></script>
    <script src="/static/js/FileSaver.min.js"></script>
    <script src="/static/js/xlsx.full.min.js"></script>
    <script type="text/javascript">

        var url;
        // 屏蔽右键 变为菜单那
        $(function () {
            $(document).bind('contextmenu', function (e) {
                e.preventDefault();
                $('#tree-0').menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            });
        });

        //项目列表选择
        function openProjListChoose() {
            $("#projList").dialog("open").dialog("setTitle", "进入项目选择");
            $("#projTree").tree({
                url: '/admin/project/loadProjectTree',
                onLoadSuccess: function (node, data) {
                    $("#tree").tree('expandAll');
                },
                //右键选择树形菜单操作
                onContextMenu: function (e, node) { //右键时触发事件                      
                    e.preventDefault(); //阻止浏览器捕获右键事件
                    var rootNodes = $("#tree").tree('getRoots');
                    for (var i = 0; i < rootNodes.length; i++) {
                        var theNode = $("#tree").tree('find', rootNodes[i].id);
                        $("#tree").tree('uncheck', theNode.target);
                    }
                    $("#tree").tree('select', node.target);
                    if (node.attributes.state == 2) {
                        $("#tree-2").menu('show', {
                            left: e.pageX,//在鼠标点击处显示菜单
                            top: e.pageY
                        });
                    } else if (node.attributes.state == 1) {
                        $("#tree-1").menu('show', {
                            left: e.pageX,//在鼠标点击处显示菜单
                            top: e.pageY
                        });
                    } else {
                        $('#tree-0').menu('show', {
                            left: e.pageX,//在鼠标点击处显示菜单
                            top: e.pageY
                        });
                    }

                },
                //单击某类别 展示不同详细和按钮选择
                onClick: function (node) {
                    if (node.attributes.state == 2) { // 假如是二级菜单,可以删除
                        $("#projNameList").datagrid('load', {
                            "w_id": node.id
                        });
                    } else if (node.attributes.state == 1) {
                        $("#projNameList").datagrid('load', {
                            "o_id": node.id
                        });
                    }
                }
            });
            loadDataGrid();
        }

        //加载项目数据表格
        function loadDataGrid() {
            $("#projNameList").datagrid({
                //双击事件
                onDblClickRow: function (index, row) {
                    $("#projId").val(row.id);
                    $("#projName").val(row.xiangmumingcheng);
                    $("#projList").dialog("close");
                },
                onRowContextMenu: function (e, rowIndex, rowData) { //右键时触发事件                      
                    e.preventDefault(); //阻止浏览器捕获右键事件
                    $(this).datagrid("clearSelections"); //取消所有选中项
                    $(this).datagrid("selectRow", rowIndex); //根据索引选中该行
                    $('#grid-data').menu('show', {
                        left: e.pageX,//在鼠标点击处显示菜单
                        top: e.pageY
                    });
                    e.preventDefault();  //阻止浏览器自带的右键菜单弹出
                },
            });
        }

        //仓库列表选择
        function openStoreListChoose() {
            $("#storeList").dialog("open").dialog("setTitle", "进入仓库选择");
            $("#storeTree").tree({
                url: '/admin/kufang/loadKufangTree',
                onLoadSuccess: function (node, data) {
                    $("#storeTree").tree('expandAll');
                },
                //右键选择树形菜单操作
                onContextMenu: function (e, node) { //右键时触发事件                      
                    e.preventDefault(); //阻止浏览器捕获右键事件
                    var rootNodes = $("#tree").tree('getRoots');
                    for (var i = 0; i < rootNodes.length; i++) {
                        var theNode = $("#tree").tree('find', rootNodes[i].id);
                        $("#tree").tree('uncheck', theNode.target);
                    }
                    $("#tree").tree('select', node.target);
                    if (node.attributes.state == 2) {
                        $("#tree-2").menu('show', {
                            left: e.pageX,//在鼠标点击处显示菜单
                            top: e.pageY
                        });
                    } else if (node.attributes.state == 1) {
                        $("#tree-1").menu('show', {
                            left: e.pageX,//在鼠标点击处显示菜单
                            top: e.pageY
                        });
                    } else {
                        $('#tree-0').menu('show', {
                            left: e.pageX,//在鼠标点击处显示菜单
                            top: e.pageY
                        });
                    }

                },
                //单击某类别 展示不同详细和按钮选择
                onClick: function (node) {
                    if (node.attributes.state == 2) { // 假如是二级菜单,可以删除
                        $("#storeNameList").datagrid('load', {
                            "w_id": node.id
                        });
                    } else if (node.attributes.state == 1) {
                        $("#storeNameList").datagrid('load', {
                            "o_id": node.id
                        });
                    }
                }
            });
            loadKufangDataGrid();
        }

        //加载项目数据表格
        function loadKufangDataGrid() {
            $("#storeNameList").datagrid({
                //双击事件
                onDblClickRow: function (index, row) {
                    $("#storeId").val(row.id);
                    $("#storeName").val(row.kufangmingcheng);
                    $("#storeList").dialog("close");
                },
                onRowContextMenu: function (e, rowIndex, rowData) { //右键时触发事件                      
                    e.preventDefault(); //阻止浏览器捕获右键事件
                    $(this).datagrid("clearSelections"); //取消所有选中项
                    $(this).datagrid("selectRow", rowIndex); //根据索引选中该行
                    $('#grid-data').menu('show', {
                        left: e.pageX,//在鼠标点击处显示菜单
                        top: e.pageY
                    });
                    e.preventDefault();  //阻止浏览器自带的右键菜单弹出
                },
            });
        }

        //加载项目tree类别
        function openDeptListChoose() {
            $("#deptList").dialog("open").dialog("setTitle", "选择发料单位");
            $("#deptTree").tree({
                url: '/admin/bumen/loadBumenTree',
                onLoadSuccess: function (node, data) {
                    $("#deptTree").tree('expandAll');
                },
                onDblClick: function (node) {

                },
                //右键选择树形菜单操作
                onContextMenu: function (e, node) { //右键时触发事件                      
                    e.preventDefault(); //阻止浏览器捕获右键事件
                },
                //单击某类别 展示不同详细和按钮选择
                onClick: function (node) {
                    if (node.attributes.state == 2) { // 假如是二级菜单,可以删除
                        $("#deptNameList").datagrid('load', {
                            "w_id": node.id
                        });
                    } else if (node.attributes.state == 1) {
                        $("#deptNameList").datagrid('load', {
                            "o_id": node.id
                        });
                    }
                }
            });
            loadDeptDataGrid();
        }

        //加载部门数据表格
        function loadDeptDataGrid() {
            $("#deptNameList").datagrid({
                //双击事件
                onDblClickRow: function (index, row) {
                    $("#deptId").val(row.id);
                    $("#deptName").val(row.bumenmingcheng);
                    $("#deptList").dialog("close");
                },
                onRowContextMenu: function (e, rowIndex, rowData) { //右键时触发事件                      
                    e.preventDefault(); //阻止浏览器捕获右键事件
                },
            });
        }

        // 查询项目
        function searchProj() {
            $("#projNameList").datagrid('load', {
                "xiangmumingcheng": $("#projSearch").val()
            })
        }

        // 查询部门
        function searchDept() {
            $("#deptNameList").datagrid('load', {
                "bumenmingcheng": $("#deptSearch").val()
            })
        }

        //查询库房
        function searchStore() {
            $("#storeNameList").datagrid('load', {
                "kufangmingcheng": $("#storeSearch").val()
            })
        }

        //保存项目选择
        function saveProjChoose() {
            var node = $('#projTree').tree('getSelected'); // 获取选中节点
            $("#projId").val(node.id);
            $("#projName").val(node.text);
            $("#projList").dialog("close");
        }

        //保存部门选择 条目->文本
        function saveDeptChoose() {
            var node = $('#deptTree').tree('getSelected'); // 获取选中节点
            $("#deptId").val(node.id);
            $("#deptName").val(node.text);
            $("#deptList").dialog("close");
        }

        //保存库房选择
        function saveStoreChoose() {
            var node = $('#storeTree').tree('getSelected');
            $("#storeId").val(node.id);
            $("#storeName").val(node.text);
            $("#storeList").dialog("close");
        }


        $(document).ready(function () {
            $("#s_btime").datebox("setValue", genLastMonthDayStr()); // 设置上个月日期
            $("#s_etime").datebox("setValue", genTodayStr()); // 设置当前日期

            $('#dg').datagrid({
                onClickRow: function (index, row) {
                    $("#dg2").datagrid({
                        url: '/admin/yanshoudan/listDetailsByZId',
                        queryParams: {
                            zongzhangid: row.id
                        },
                        onRowContextMenu: function (e, rowIndex, rowData) { //右键时触发事件                      
                            e.preventDefault(); //阻止浏览器捕获右键事件
                            $(this).datagrid("clearSelections"); //取消所有选中项
                            $(this).datagrid("selectRow", rowIndex); //根据索引选中该行
                            $('#grid-data').menu('show', {
                                left: e.pageX,//在鼠标点击处显示菜单
                                top: e.pageY
                            });
                            e.preventDefault();  //阻止浏览器自带的右键菜单弹出
                        },
                    });
                },
                onRowContextMenu: function (e, rowIndex, rowData) { //右键时触发事件                      
                    e.preventDefault(); //阻止浏览器捕获右键事件
                    $(this).datagrid("clearSelections"); //取消所有选中项
                    $(this).datagrid("selectRow", rowIndex); //根据索引选中该行
                    $('#grid-data').menu('show', {
                        left: e.pageX,//在鼠标点击处显示菜单
                        top: e.pageY
                    });
                    e.preventDefault();  //阻止浏览器自带的右键菜单弹出
                },
            });

        });

        //搜索验收单
        function searchYanshoudan() {
            $('#dg2').datagrid('loadData', {total: 0, rows: []});
            var projName = $("#projName").val();
            var xiangmumingcheng;
            if (projName.length == 0) {
                xiangmumingcheng = "";
            } else {
                xiangmumingcheng = projName;
            }

            var deptName = $("#deptName").val();
            var faliaodanwei;
            if (deptName.length == 0) {
                faliaodanwei = "";
            } else {
                faliaodanwei = deptName;
            }

            var storeName = $("#storeName").val();
            var rukukufang;
            if (storeName.length == 0) {
                rukukufang = "";
            } else {
                rukukufang = storeName;
            }

            $("#dg").datagrid({
                url: '/admin/yanshoudan/getSelective',
                queryParams: {
                    wuliaoyanshoudanhao: $("#s_danhao").val(),
                    xiangmumingcheng: xiangmumingcheng,
                    faliaodanwei: faliaodanwei,
                    rukukufang: rukukufang,
                    s_btime: $("#s_btime").datebox("getValue"),
                    s_etime: $("#s_etime").datebox("getValue")
                },
                onRowContextMenu: function (e, rowIndex, rowData) { //右键时触发事件                      
                    e.preventDefault(); //阻止浏览器捕获右键事件
                    $(this).datagrid("clearSelections"); //取消所有选中项
                    $(this).datagrid("selectRow", rowIndex); //根据索引选中该行
                    $('#grid-data').menu('show', {
                        left: e.pageX,//在鼠标点击处显示菜单
                        top: e.pageY
                    });
                    e.preventDefault();  //阻止浏览器自带的右键菜单弹出
                },
            });
        }

        //删除到货单
        function deleteYanshoudan() {
            var selectedRows = $("#dg").datagrid("getSelections");
            if (selectedRows.length == 0) {
                $.messager.alert("系统提示", "请选择要删除的到货单！");
                return;
            }
            var ids = new Array();
            if (selectedRows.length > 1) {
                for (var i = 0; i < selectedRows.length; i++) {
                    ids.push(selectedRows[i].id);
                }
            } else if (selectedRows.length == 1) {
                var id = selectedRows[0].id;
                ids.push(id);
            }
            $.messager.confirm("系统提示", "<font color=red>删除到货单后将无法恢复，是否删除？</font>", function (r) {
                if (r) {
                    $.post("/admin/yanshoudan/delete", {'ids': ids}, function (result) {
                        if (result.success) {
                            $("#dg").datagrid("reload");
                            $("#dg2").datagrid("reload");
                        } else {
                            $.messager.alert("系统提示", result.errorInfo);
                        }
                    }, "json");
                }
            });
        }

    </script>
</head>
<body class="easyui-layout" style="margin: 1px">
<div region="north" style="height: 350px;">
    <table id="dg" title="验收单据查询" class="easyui-datagrid" fit=true
           fitColumns="true" pagination="true" rownumbers="true" singleSelect="true"
           toolbar="#tb">
        <thead>
        <tr>
            <!--展示验收单主表-->
            <!--<th field="ukyo" align="center" width="30" checkbox="true"></th>-->
            <th field="id" width="30" align="center" sortable="true" hidden="true">id</th>
            <th field="wuliaoyanshoudanhao" width="80" align="center" sortable="true">验收单单号</th>
            <th field="xiangmumingcheng" width="50" align="center" sortable="true">项目名称</th>
            <th field="faliaodanwei" width="50" align="center" sortable="true">发料单位</th>
            <th field="rukukufang" width="50" align="center" sortable="true">入库库房</th>
            <th field="daohuoshijian" width="50" align="center" sortable="true">到货时间</th>
            <th field="yanshouriqi" width="50" align="center" sortable="true">验收时间</th>
            <th field="chengyunchehao" width="50" align="center" sortable="true">承运车号</th>
            <th field="yundanhao" width="50" align="center" sortable="true">运单号</th>
            <th field="beizhu" width="50" align="center" sortable="true">备注</th>
            <th field="founder" width="50" align="center" sortable="true">创建人</th>
            <th field="createtime" width="70" align="center" sortable="true">创建时间</th>
        </tr>
        </thead>
    </table>

    <div id="tb">
        <div style="padding-bottom: 5px">
            <fieldset style="border-color: #E7F0FF">
                <legend>查询设置</legend>
                &nbsp;单据号：&nbsp;
                <input type="text" id="s_danhao" name="wuliaoyanshoudanhao" size="15"
                       onkeydown="if(event.keyCode==13) searchYanshoudan()"/>
                &nbsp;&nbsp;&nbsp;&nbsp;项目选择：<input type="text" id="projName" name="xiangmumingcheng"
                                                    style="width: 90px;" readonly="readonly"
                                                    onclick="openProjListChoose()" required="true"/>
                <input type="hidden" id="projId"/>
                <!--供应商：<select class="easyui-combobox"  id="supplierId" name="supplier.id" style="width: 200px;" required=true data-options="required:true,panelHeight:'auto',valueField:'id',textField:'name',url:'/admin/supplier/comboList'"></select>-->
                &nbsp;&nbsp;&nbsp;&nbsp;发料单位：<input type="text" id="deptName" name="bumenmingcheng" size="10"
                                                    readonly="readonly" onclick="openDeptListChoose()"
                                                    required="true"/>
                <input type="hidden" id="deptId"/>
                &nbsp;&nbsp;&nbsp;&nbsp;入库库房：<input type="text" id="storeName" name="rukukufang" size="10"
                                                    readonly="readonly" onclick="openStoreListChoose()"
                                                    required="true"/>
                <input type="hidden" id="storeId"/>
                <!-- -->
                <br>
                &nbsp;&nbsp;&nbsp;日期：&nbsp;
                <input id="s_btime" name="s_btime" class="easyui-datebox" editable=false style="width:100px">
                &nbsp;&nbsp;-&nbsp;&nbsp;
                <input id="s_etime" name="s_etime" class="easyui-datebox" editable=false style="width:100px">
                &nbsp;&nbsp;<a href="javascript:searchYanshoudan()" class="easyui-linkbutton" iconCls="icon-search"
                               plain="true">搜索</a>
                &nbsp;&nbsp;<a href="javascript:deleteYanshoudan()" class="easyui-linkbutton" iconCls="icon-remove"
                               plain="true">删除</a>
                &nbsp;&nbsp;<a href="javascript:printYanshouDan()" class="easyui-linkbutton" iconCls="icon-print"
                               plain="true">打印</a>
            </fieldset>
        </div>
    </div>
</div>
<div region="center" style="margin-top: 5px;">
    <table id="dg2" class="easyui-datagrid" fit=true pageList=[10,20,30,40,50,1000000]
           fitColumns="true" rownumbers="true" pagination="true" toolbar="#tbzi">
        <!--&gt;-->
        <thead>
        <tr>
            <!--展示验收单详细-->
            <!--<th field="ukyo" align="center" width="30" checkbox="true"></th>-->
            <th field="id" width="60" align="center" sortable="true" hidden="hidden">编号</th>
            <th field="duiyingdaohuodanhao" width="80" align="center" sortable="true">到货单号</th>
            <th field="wuliaobianma" width="75" align="center" sortable="true">物料编码</th>
            <th field="mingchengguige" width="100" align="center" sortable="true">名称规格</th>
            <th field="yingshoushuliang" width="50" align="center" sortable="true">应收数量</th>
            <th field="shishoushuliang" width="50" align="center" sortable="true">实收数量</th>
            <th field="danwei" width="40" align="center" sortable="true">单位</th>
            <th field="danjia" width="60" align="center" sortable="true">单价</th>
            <th field="zongjia" width="60" align="center" sortable="true">总价</th>
            <th field="beizhu" width="50" align="center" sortable="true">备注</th>
            <th field="createtime" width="70" align="center" sortable="true">创建时间</th>
        </tr>
        </thead>
    </table>
    <div id="tbzi">
        <div style="padding-bottom: 5px">
            <fieldset style="border-color: #E7F0FF">
                <legend>明细导出</legend>
                <a href="javascript:void(0)" id="export_excel" iconCls="icon-save" class="easyui-linkbutton"
                   plain="true">保存数据</a>
                - 点击'保存数据'导出下方明细
            </fieldset>
        </div>
    </div>
</div>

<!--查询项目列表-->
<div id="projList" class="easyui-dialog" style="width:800px;height:460px;"
     closed="true" data-options="onClose:function(){$('#s_codeOrProjName').val('');}">
    <div class="easyui-layout" style="width:100%;height:100%;">

        <div region="north" style="height: 50px;">
            <div style="padding-top: 8px;padding-left: 40px;">
                &nbsp;项目名称：&nbsp;<input type="text" id="projSearch" size="30"
                                              onkeydown="if(event.keyCode==13) searchProj()"/>
                <a href="javascript:searchProj()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
            </div>
        </div>
        <div region="west" style="width: 200px" title="项目分类" split="true" data-options="collapsible:false">
            <div style="overflow-x: auto; overflow-y: auto; height: 90%; width:98%;">
                <ul id="projTree" class="easyui-tree" style="padding: 10px"></ul>
            </div>
            <div style="position:absolute;bottom: 10px;left: 15px">
                <a href="javascript:saveProjChoose()" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">选择</a>
                <!--<a id="del" href="javascript:deleteGoodsType()" class="easyui-linkbutton" data-options="iconCls:'icon-remove',disabled:true">删除</a>-->
            </div>
        </div>
        <div region="center">
            <table id="projNameList" title="项目列表" class="easyui-datagrid"
                   fitColumns="true" pagination="true" rownumbers="true" singleSelect="true"
                   url="/admin/dproject/getSelective" fit="true" toolbar="#tb4">
                <thead>
                <tr>
                    <th field="id" width="30" hidden=true align="center">编号</th>
                    <th field="xiangmumingcheng" width="150" align="center">项目名称</th>
                    <th field="createtime" width="90" align="left">创建时间</th>

                </tr>
                </thead>
            </table>
            <div id="tb4">
                <div>
                    <a href="javascript:saveProjChoose()" class="easyui-linkbutton" iconCls="icon-ok"
                       plain="true">选择</a>
                </div>
            </div>
        </div>
    </div>
</div>

<!--查询仓库列表-->
<div id="storeList" class="easyui-dialog" style="width:800px;height:460px;"
     closed="true" data-options="onClose:function(){$('#s_codeOrStoreName').val('');}">
    <div class="easyui-layout" style="width:100%;height:100%;">

        <div region="north" style="height: 50px;">
            <div style="padding-top: 8px;padding-left: 40px;">
                &nbsp;仓库名称：&nbsp;<input type="text" id="storeSearch" size="30"
                                              onkeydown="if(event.keyCode==13) searchStore()"/>
                <a href="javascript:searchStore()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
            </div>
        </div>
        <div region="west" style="width: 200px" title="仓库分类" split="true" data-options="collapsible:false">
            <div style="overflow-x: auto; overflow-y: auto; height: 90%; width:98%;">
                <ul id="storeTree" class="easyui-tree" style="padding: 10px"></ul>
            </div>
            <div style="position:absolute;bottom: 10px;left: 15px">
                <a href="javascript:saveStoreChoose()" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">选择</a>
                <!--<a id="del" href="javascript:deleteGoodsType()" class="easyui-linkbutton" data-options="iconCls:'icon-remove',disabled:true">删除</a>-->
            </div>
        </div>
        <div region="center">
            <table id="storeNameList" title="仓库列表" class="easyui-datagrid"
                   fitColumns="true" pagination="true" rownumbers="true" singleSelect="true"
                   url="/admin/dkufang/getSelective" fit="true" toolbar="#tb5">
                <thead>
                <tr>
                    <th field="id" width="30" hidden=true align="center">编号</th>
                    <th field="kufangmingcheng" width="150" align="center">仓库名称</th>
                    <th field="createtime" width="90" align="left">创建时间</th>

                </tr>
                </thead>
            </table>
            <div id="tb5">
                <div>
                    <a href="javascript:saveStoreChoose()" class="easyui-linkbutton" iconCls="icon-ok"
                       plain="true">选择</a>
                </div>
            </div>
        </div>
    </div>
</div>

<!--查询部门列表-->
<div id="deptList" class="easyui-dialog" style="width:800px;height:460px;"
     closed="true" data-options="onClose:function(){$('#s_codeOrDeptName').val('');}">
    <div class="easyui-layout" style="width:100%;height:100%;">

        <div region="north" style="height: 50px;">
            <div style="padding-top: 8px;padding-left: 40px;">
                &nbsp;部门名称：&nbsp;<input type="text" id="deptSearch" size="30"
                                              onkeydown="if(event.keyCode==13) searchDept()"/>
                <a href="javascript:searchDept()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
            </div>
        </div>
        <div region="west" style="width: 200px" title="发料单位分类" split="true" data-options="collapsible:false">
            <div style="overflow-x: auto; overflow-y: auto; height: 90%; width:98%;">
                <ul id="deptTree" class="easyui-tree" style="padding: 10px"></ul>
            </div>
            <div style="position:absolute;bottom: 10px;left: 15px">
                <a href="javascript:saveDeptChoose(1)" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">选择</a>
                <!--<a id="del" href="javascript:deleteGoodsType()" class="easyui-linkbutton" data-options="iconCls:'icon-remove',disabled:true">删除</a>-->
            </div>
        </div>
        <div region="center">
            <table id="deptNameList" title="发料单位列表" class="easyui-datagrid"
                   fitColumns="true" pagination="true" rownumbers="true" singleSelect="true"
                   url="/admin/dbumen/getSelective" fit="true" toolbar="#tb-bm">
                <thead>
                <tr>
                    <th field="id" width="30" hidden=true align="center">编号</th>
                    <th field="bumenmingcheng" width="150" align="center">单位名称</th>
                    <th field="createtime" width="90" align="left">创建时间</th>
                </tr>
                </thead>
            </table>
            <div id="tb-bm">
                <div>
                    <a href="javascript:saveDeptChoose(2)" class="easyui-linkbutton" iconCls="icon-ok"
                       plain="true">选择</a>
                </div>
            </div>
        </div>
    </div>
</div>

<!--大屏幕上右键datagrid dlg-->
<div id="grid-data" class="easyui-menu" style="width: 80px; display: none;">
    <!--<div id="grid-data-edit" href="javascript:openDetailsModifyDialog()" data-options="iconCls:'icon-edit'">修改</div>-->
    <!--<div id="grid-data-delete" href="javascript:deleteDetails()" data-options="iconCls:'icon-remove'" plain="true">删除-->
    <!--</div>-->
</div>
<!--二级菜单右键弹出-->
<div id="tree-2" class="easyui-menu" style="width: 80px; display: none;">
    <!--<div id="tree-2-delete" href="javascript:deleteType()" data-options="iconCls:'icon-remove'">删除二级菜单</div>-->
</div>
<!--一级菜单右键弹出-->
<div id="tree-1" class="easyui-menu" style="width: 80px; display: none;">
    <!--<div id="tree-1-add-2" href="javascript:openTypeAddDialog()" data-options="iconCls:'icon-add'">添加二级菜单</div>-->
    <!--<div id="tree-1-delete" href="javascript:deleteType()" data-options="iconCls:'icon-remove'">删除一级菜单</div>-->
</div>
<!--树形菜单右键弹出-->
<div id="tree-0" class="easyui-menu" style="width: 80px; display: none;">
    <!--<div id="tree-0-add-1" href="javascript:openTypeAddDialog(0)" data-options="iconCls:'icon-add'">添加一级菜单</div>-->
    <!--<div id="tree-0-refresh" href="javascript:reloadTree()" data-options="iconCls:'icon-reload'">重新加载类别</div>-->
</div>


</body>

<script>
    function printYanshouDan() {
        var selectedRows = $("#dg").datagrid("getSelections");
        if (selectedRows.length == 0) {
            $.messager.alert("系统提示", "请选择要打印的验收单！");
            return;
        }
        var detailsRows = $("#dg2").datagrid("getRows"); //当前页所有行
        if (detailsRows.length == 0) {
            $.messager.alert("系统提示", "该单据无详细数据！");
            return;
        }
        // var headTitle = [];
        // var footTitle = [];
        // headAndFootPrintArgs.push(headTitle);
        // headAndFootPrintArgs.push(footTitle);
        createFormPage("物 料 验 收 单", detailsRows, selectedRows, "验收打印");
    }

    // printName 打印任务名
    // printDatagrid 要打印的datagrid
    // strPrintName 打印任务名
    // printDatagrid 要打印的datagrid
    function createFormPage(strPrintName, detailsRows, selectedRows, printEnum) {
        //单据表头左侧
        var tableString = ' <div class="box">\n' +
            '        <div class="left">\n' +
            '            <div>\n' +
            '                <p><strong>发料单位</strong></p>\n' +
            '                <p>' + selectedRows[0].faliaodanwei + '</p>\n' +
            '            </div>\n' +
            '            <div>\n' +
            '                <p><strong>承运车号</strong></p>\n' +
            '                <p>' + selectedRows[0].chengyunchehao + '</p>\n' +
            '            </div>\n' +
            '            <div>\n' +
            '                <p><strong>运&nbsp;&nbsp;单&nbsp;&nbsp;号</strong></p>\n' +
            '                <p>' + selectedRows[0].yundanhao + '</p>\n' +
            '            </div>\n' +
            '        </div>'
        //单据中部 title处
        tableString += '<div class="center">\n' +
            '            <div class="center_left">\n' +
            '                <strong><p>山 东 海 盛 海 洋<br><br><br>工程集团有限公司</p></strong>\n' +
            '            </div>\n' +
            '            <div class="center_right">\n' +
            '                <p>(</p>\n' +
            '                <p></p>\n' +
            '                <p>)</p>\n' +
            '            </div>\n' +
            '            <div class="center_bottom">\n' +
            '                <strong>\n' +
            '                    <p>器&nbsp;&nbsp;&nbsp;&nbsp;材&nbsp;&nbsp;&nbsp;&nbsp;验&nbsp;&nbsp;&nbsp;&nbsp;收&nbsp;&nbsp;&nbsp;&nbsp;单</p>\n' +
            '                </strong>\n' +
            '            </div>\n' +
            '        </div>';
        //右侧,有数据动态填入
        tableString += '      <div class="right left">\n' +
            '            <div>\n' +
            '                <p><strong>本单编号</strong></p>\n' +
            '                <p>' + selectedRows[0].wuliaoyanshoudanhao + '</p>\n' +
            '            </div>\n' +
            '            <div>\n' +
            '                <p><strong>到货日期</strong></p>\n' +
            '                <p>' + selectedRows[0].daohuoshijian + '</p>\n' +
            '            </div>\n' +
            '            <div>\n' +
            '                <p><strong>验收日期</strong></p>\n' +
            '                <p>' + selectedRows[0].yanshouriqi + '</p>\n' +
            '            </div>\n' +
            '        </div>\n' +
            '    </div>';

        //单据表头下的其他信息,不在table格子中的,格子之上
        if (printEnum == '验收打印') {
            console.log("验收打印");
        }
        //打印详情
        //start sub datagrid
        tableString += '    <table cellpadding="0" cellspacing="0" border="0">\n' +
            '        <thead>\n' +
            '            <tr>\n' +
            '                <td rowspan="2">器材编号</td>\n' +
            '                <td rowspan="2">器材名称及规格</td>\n' +
            '                <td rowspan="2">计量单位</td>\n' +
            '                <td colspan="2">数量</td>\n' +
            '                <td colspan="2">价格</td>\n' +
            '                <td rowspan="2">验收记录</td>\n' +
            '            </tr>\n' +
            '            <tr>\n' +
            '                <td>应收</td>\n' +
            '                <td>实收</td>\n' +
            '                <td>单价</td>\n' +
            '                <td>总价</td>\n' +
            '            </tr>\n' +
            '        </thead>\n' +
            '        <tbody>';
        //开始循环获取详情数据
        for (var i = 0; i < detailsRows.length; ++i) {
            var danjia1 = detailsRows[i].danjia == null?'':detailsRows[i].danjia;
            var zongjia1 = detailsRows[i].zongjia == null?'':detailsRows[i].zongjia;
            tableString += '<tr>';
            tableString += '<td>' + detailsRows[i].wuliaobianma + '</td>';
            tableString += '<td>' + detailsRows[i].mingchengguige + '</td>';
            tableString += '<td>' + detailsRows[i].danwei + '</td>';
            tableString += '<td>' + detailsRows[i].yingshoushuliang + '</td>';
            tableString += '<td>' + detailsRows[i].shishoushuliang + '</td>';
            tableString += '<td>' + danjia1 + '</td>';
            tableString += '<td>' + zongjia1 + '</td>';
            tableString += '<td>' + detailsRows[i].beizhu + '</td>';
            tableString += '</tr>';
        }
        tableString += '</tbody>\n' +
            '    </table>';
        //签字人
        tableString += '<div class="bottom_box">\n' +
            '        <div>\n' +
            '            <p><strong>主管:</strong></p>\n' +
            '            <p></p>\n' +
            '        </div>\n' +
            '        <div>\n' +
            '            <p><strong>保管员:</strong></p>\n' +
            '            <p></p>\n' +
            '        </div>\n' +
            '        <div>\n' +
            '            <p><strong>验收:</strong></p>\n' +
            '            <p></p>\n' +
            '        </div>\n' +
            '        <div>\n' +
            '            <p><strong>制单:</strong></p>\n' +
            '            <p>'+selectedRows[0].founder+'</p>\n' +
            '        </div>\n' +
            '    </div>';

        localStorage.setItem("tableString", tableString);
        window.location.href = "print.html";
        // window.open("print.html", tableString,"location=no, status=no;help:no;toolbar=no, menubar=no, scrollbars=no, resizeable=no,dialogWidth:800px;top:0;left:0;dialogHeight:600px;scroll:auto;");
    }

    // var printConfig = {
    //     companyName: 'HS',
    //     printTime: new Date()
    // }
</script>
<script>
    // 导出excel按钮
    $("#export_excel").click(exportExcel);

    // 导出excel
    function exportExcel() {
        //获得选中记录 多选
        var row = $('#dg2').datagrid('getRows');
        // var row = $('#dg').datagrid('getSelections');
        // 去除不需要导出的实体字段
        var deleteKey = ['updatetime',
            'zongzhangid', 'sort', 'order'
        ];
        for (var i in row) {
            for (var j in deleteKey) {
                delete row[i][deleteKey[j]];
            }
        }
        if (row) {
            $.messager.confirm('确认按钮', '你确定要导出本页验收明细数据?', function (r) {
                if (r) {
                    downloadExl(row);
                }
            })
        }
    }

    function downloadExl(data, type) {
        var keys = Object.keys(data[0]);
        var firstRow = {};
        keys.forEach(function (item) {
            console.log("item:" + item);
            firstRow[item] = item;
        });
        data.unshift(firstRow);

        var content = {};

        //把json格式的数据转换为excel的行列形式
        var sheetsData = data.map(function (item, rowIndex) {
            return keys.map(function (key, columnIndex) {
                return Object.assign({}, {
                    value: item[key],
                    position: (columnIndex > 25 ? getCharCol(columnIndex) : String.fromCharCode(65 + columnIndex)) + (rowIndex + 1),
                });
            });
        }).reduce(function (prev, next) {
            return prev.concat(next);
        });

        sheetsData.forEach(function (item, index) {
            // content[item.position] = {v: item.value};
            if (item.value == 'id') {
                content[item.position] = {v: '序列号'};
            } else if (item.value == 'duiyingdaohuodanhao') {
                content[item.position] = {v: '到货单号'};
            } else if (item.value == 'wuliaobianma') {
                content[item.position] = {v: '物料编码'};
            } else if (item.value == 'mingchengguige') {
                content[item.position] = {v: '名称规格'};
            } else if (item.value == 'yingshoushuliang') {
                content[item.position] = {v: '应收数量'};
            } else if (item.value == 'shishoushuliang') {
                content[item.position] = {v: '实收数量'};
            } else if (item.value == 'danwei') {
                content[item.position] = {v: '单位'};
            } else if (item.value == 'danjia') {
                content[item.position] = {v: '单价'};
            } else if (item.value == 'zongjia') {
                content[item.position] = {v: '总价'};
            } else if (item.value == 'beizhu') {
                content[item.position] = {v: '备注'};
            } else if (item.value == 'createtime') {
                content[item.position] = {v: '创建时间'};
            } else {
                if (item.value == null || item.value == undefined || item.value == 'null') {
                    content[item.position] = {v: ''};  //remember
                } else {
                    content[item.position] = {v: item.value};  //remember
                }
            }
        });

        //设置区域,比如表格从A1到D10,SheetNames:标题
        var coordinate = Object.keys(content);
        var workBook = {
            SheetNames: ["1"],
            Sheets: {
                "1": Object.assign({}, content, {"!ref": coordinate[0] + ":" + coordinate[coordinate.length - 1]}),
            }
        };

        //这里的数据是用来定义导出的格式类型
        var excelData = XLSX.write(workBook, {bookType: "xlsx", bookSST: false, type: "binary"});
        var blob = new Blob([string2ArrayBuffer(excelData)], {type: ""});

        // filename = "石油焦";
        $.messager.prompt("输入框", "请输入保存文件名.", function (filename) {
            if (filename) {
                var nowTime = new Date().toLocaleDateString();
                saveAs(blob, filename + "-验收明细" + nowTime + ".xlsx"); //文件名
            }
        });
    }

    //字符串转字符流
    function string2ArrayBuffer(s) {
        var buf = new ArrayBuffer(s.length);
        var view = new Uint8Array(buf);
        for (var i = 0; i != s.length; ++i) {
            view[i] = s.charCodeAt(i) & 0xFF;
        }
        return buf;
    }

    //将指定的自然数转换为26进制表示. 映射关系: [0-25] -> [A-Z]
    function getCharCol(n) {
        var temCol = "",
            s = "",
            m = 0;
        while (n > 0) {
            m = n % 26 + 1;
            s = String.fromCharCode(m + 64) + s;
            n = (n - m) / 26;
        }
        return s;
    }

</script>
</html>